//
//  BGMyTenderTableViewController.m
//  bigMall
//
//  Created by LTOVE on 16/8/18.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGMyTenderTableViewController.h"
#import "MJExtension.h"

#import "BGDateResult.h"
#import "BGDateFormaTool.h"
#import "BGMyTenderModel.h"
#import "BGMyTendetListResultModel.h"
#import "BGMyTenderTableViewCell.h"
#import "BGOwnInviteDownModel.h"
#import "MJRefresh.h"
#import "BGOwnInviteBidLogsVC.h"


#import "BGBuyerCenterController.h"

#import "BGOwnInviteDelModel.h"
#import "BGSellerReturnMaginModel.h"
#import "BGAddInviteOneTableViewController.h"

#import "MBProgressHUD+MJ.h"
#import "MBProgressHUD.h"

@interface BGMyTenderTableViewController ()<BGMyTenderTableViewCellDelegate>{
    
    NSMutableArray *_dataArray;
    NSUInteger _thisPage;
    NSUInteger _allPage;
}

@end

@implementation BGMyTenderTableViewController

//- (instancetype)init
//{
//    if (self = [super init]) {
//        
//        UIBarButtonItem *bac = [[UIBarButtonItem alloc]initWithTitle:@"<返回" style:UIBarButtonItemStylePlain target:self action:@selector(bacBtnClick)];
//        [bac setImage:[UIImage imageNamed:@"返回icon"]];
////        bac.title = @"fanhui";
//////        [bac setAction:@selector(bacBtnClick)];
////        [bac setTarget:self];
////        [bac setAction:@selector(bacBtnClick)];
//        self.navigationItem.leftBarButtonItem = bac;
//    }
//    return self;
//}

- (void)bacBtnClick
{
    GWLog(@"点击返回安阿牛");
    for (UIViewController *vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:[BGBuyerCenterController class]]) {
            [self.navigationController popToViewController:vc animated:YES];
        }
    }
}
-(void)loadMoreData
{
    if (_thisPage > _allPage) {
        
        [self.tableView.footer endRefreshingWithNoMoreData];
        
        return;
    }
    [MBProgressHUD showMessage:@"加载中..."];
    [BGDateResult ownInviteListWithThisPage:[NSString stringWithFormat:@"%lud",(unsigned long)_thisPage]
                                    pageNum:@"10"
                                    success:^(NSObject *result) {
                                        [MBProgressHUD hideHUD];
                                        BGMyTendetListResultModel *models = (BGMyTendetListResultModel *)result;
                                        [self.tableView.footer endRefreshing];
                                        //                                        NSArray *array = models.list;
                                        [_dataArray addObjectsFromArray:models.list];
//                                        _dataArray = [[NSMutableArray alloc]initWithArray:models.list];
//                                        [self.tableView.footer endRefreshing];
                                        [self.tableView reloadData];
                                        
                                        if (models.list.count) {
                                            _thisPage ++;
                                        }
                                        
                                    } failure:^(NSError *error) {
                                        [MBProgressHUD hideHUD];
                                    }];

    
}
- (void)loadData
{
    [MBProgressHUD showMessage:@"加载中..."];
    [BGDateResult ownInviteListWithThisPage:@"1"
                                    pageNum:@"10"
                                    success:^(NSObject *result) {
                                        [MBProgressHUD hideHUD];
                                        BGMyTendetListResultModel *models = (BGMyTendetListResultModel *)result;
                                        [self.tableView.header endRefreshing];
                                        //                                        NSArray *array = models.list;
//                                        [_dataArray addObjectsFromArray:models.list];
                                        
                                        _dataArray = [[NSMutableArray alloc]initWithArray:models.list];
                                       
//                                        [self.tableView.header endRefreshing];
                                        if ([models.allPage integerValue] > _thisPage) {
                                            self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
                                            _thisPage = 1;
                                            _allPage = [models.allPage integerValue];
                                        }
                                        [self.tableView reloadData];
                                        
                                        if (models.list.count) {
                                            _thisPage ++;
                                        }
                                        
                                    } failure:^(NSError *error) {
                                        [MBProgressHUD hideHUD];
                                    }];
    

}
/**
 *  按钮点击的代理
 *
 *  @param btn btn description
 */
- (void)btnClick:(UIButton *)btn
{
    NSLog(@"%@",btn.titleLabel.text);
    if ([btn.titleLabel.text isEqualToString:@"投标记录"]) {
        //投标记录
        BGOwnInviteBidLogsVC *vc = [[BGOwnInviteBidLogsVC alloc]init];
        BGMyTenderModel *model = _dataArray[btn.tag];
        vc.inviteld = model.idStr;
        [self.navigationController pushViewController:vc animated:YES];
        UIBarButtonItem *bac = [[UIBarButtonItem alloc]init];
        [bac setTitle:@""];
        self.navigationItem.backBarButtonItem = bac;
        vc.title = @"投标记录";
    }else if ([btn.titleLabel.text isEqualToString:@"下架"]){
        //下单
        [self showAlertViewWithTag:@"确定要下架吗？" actions:^{
            
            BGMyTenderModel *model = _dataArray[btn.tag];
            [BGDateResult ownInviteDownWithInviteld:model.idStr success:^(NSObject *result) {
                BGOwnInviteDownModel *resultModel = (BGOwnInviteDownModel *)result;
                if ([resultModel.status isEqualToString:@"1"]) {
                    
                    model.status = @"3";
                    [_dataArray replaceObjectAtIndex:btn.tag withObject:model];
                    NSIndexPath *index = [NSIndexPath indexPathForRow:btn.tag inSection:0];
                    [self.tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];
                    
                    //                [self.tableView reloadSections:index withRowAnimation:UITableViewRowAnimationNone];
                }
                
            } failure:^(NSError *error) {
                
                
            }];

        }];
    }else if ([btn.titleLabel.text isEqualToString:@"退还保证金"]){
        //退还保证金
        GWLog(@"ssss");
        [self showAlertViewWithTag:@"确定要退还保证金吗?" actions:^{
            BGMyTenderModel *model = _dataArray[btn.tag];
            [BGDateResult returnMarginBuyerWithGoosId:model.idStr success:^(NSObject *result) {
                BGSellerReturnMaginModel *resultModel = (BGSellerReturnMaginModel *)result;
                GWLog(@"%@",resultModel.msg);
                if ([resultModel.status isEqualToString:@"1"]) {
                    
                    model.status = @"3";
                    model.is_backed = @"1";
                    model.publish_type = @"2";
                    [_dataArray replaceObjectAtIndex:btn.tag withObject:model];
//                    NSIndexPath *index = [NSIndexPath indexPathForRow:btn.tag inSection:0];
//                    [self.tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];
                    [self.tableView reloadData];
                    
                    //                [self.tableView reloadSections:index withRowAnimation:UITableViewRowAnimationNone];
                }
                
            } failure:^(NSError *error) {
                
            }];
            
            
        }];
        
    }else if ([btn.titleLabel.text isEqualToString:@"删除"]){
        BGMyTenderModel *model = _dataArray[btn.tag];
        [self showAlertViewWithTag:@"确定要删除吗？" actions:^{
            [BGDateResult ownInviteDelWithInviteId:model.idStr success:^(NSObject *result) {
                BGOwnInviteDelModel *itemModel = (BGOwnInviteDelModel *)result;
                if ([itemModel.status isEqualToString:@"1"]) {
                    [_dataArray removeObjectAtIndex:btn.tag];
                    [self.tableView reloadData];
                }
                
            } failure:^(NSError *error) {
                
                
            }];
            
        }];
    }else return;
}
#pragma mark - ⬆️退还保证金
- (void)showAlertViewWithTag:(NSString *)tag
                      actions:(void(^)())actions
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%@",tag] message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (actions) {
            actions();
        }
//        BGMyTenderModel *model = _dataArray[tag];
//        [BGDateResult ownInviteDownWithInviteld:model.idStr success:^(NSObject *result) {
//            BGOwnInviteDownModel *resultModel = (BGOwnInviteDownModel *)result;
//            if ([resultModel.status isEqualToString:@"1"]) {
//                
//                model.status = @"3";
//                [_dataArray replaceObjectAtIndex:tag withObject:model];
//                NSIndexPath *index = [NSIndexPath indexPathForRow:tag inSection:0];
//                [self.tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];
//                
////                [self.tableView reloadSections:index withRowAnimation:UITableViewRowAnimationNone];
//            }
//            
//        } failure:^(NSError *error) {
//            
//            
//        }];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    [alertVC addAction:cancel];
    [alertVC addAction:sureAction];
    [self presentViewController:alertVC animated:YES completion:^{
        
        
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self.tableView.header beginRefreshing];
//    self.navigationController.navigationBar.delegate = self;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 154;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BGMyTenderTableViewCell *cell = [BGMyTenderTableViewCell cellWithTableView:tableView];
    BGMyTenderModel *model = _dataArray[indexPath.row];
    model.tag = indexPath.row;
    cell.model = model;
    cell.delegate = self;
    return cell;
}
#pragma mark - ⬇️ cell点击;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GWLog(@"%@",indexPath);
    BGMyTenderModel *model = _dataArray[indexPath.row];
    BGAddInviteOneTableViewController *vc = [BGAddInviteOneTableViewController new];
    vc.isYetDada = YES;
    vc.status = model.status;
    vc.goodsId = model.idStr;
    UIBarButtonItem *bac = [UIBarButtonItem new];
    bac.title = @"";
    self.navigationItem.backBarButtonItem = bac;
    vc.title = @"发布招标";
    [self.navigationController pushViewController:vc animated:YES];
}




@end
